package com.aizuda.boot.modules.business.permission.service.impl;


import com.aizuda.boot.modules.business.permission.domain.entity.RoleEntity;
import com.aizuda.boot.modules.business.permission.domain.request.PageRoleVO;
import com.aizuda.boot.modules.business.permission.domain.response.PageRoleResponseVO;
import com.aizuda.boot.modules.business.permission.mapper.RoleMapper;
import com.aizuda.boot.modules.business.permission.service.RoleService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Description: 角色实现
 *
 * @author duanyao
 * @date 2021/12/06 11:45
 *
 * <pre>
 *              悟智科技
 *      Copyright (c) 2021. All Rights Reserved.
 * </pre>
 */
@Slf4j
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleEntity> implements RoleService {

    @Autowired
    RoleMapper roleMapper;


    /**
     * 获取角色列表
     *
     * @param tenantId
     * @param page
     * @param pageRoleVO
     * @param roleIdList
     * @return
     */
    @Override
    public IPage<PageRoleResponseVO> pageRole(String  tenantId, IPage<PageRoleResponseVO> page, PageRoleVO pageRoleVO, List<Long> roleIdList) {
        List<PageRoleResponseVO> pageRoleResponseVOS = roleMapper.pageRole(tenantId,pageRoleVO, roleIdList);
        
        // 计算总记录数
        int total = pageRoleResponseVOS.size();
        // 设置总记录数
        page.setTotal(total);
        
        // 根据分页参数截取当前页数据
        int fromIndex = (int)((page.getCurrent() - 1) * page.getSize());
        int toIndex = (int)Math.min(fromIndex + page.getSize(), total);
        
        // 处理分页边界情况
        if (fromIndex < total) {
            page.setRecords(pageRoleResponseVOS.subList(fromIndex, toIndex));
        } else {
            page.setRecords(java.util.Collections.emptyList());
        }
        
        return page;
    }
}
